package com.huiyujinrong.qiangdanbao.activity.my;

import android.util.Log;

import com.huiyujinrong.qiangdanbao.BorrowHelpApp;
import com.huiyujinrong.qiangdanbao.DaggerModelComponent;
import com.huiyujinrong.qiangdanbao.bean.FaceEngineBean;
import com.huiyujinrong.qiangdanbao.bean.FaceFindBean;
import com.huiyujinrong.qiangdanbao.bean.UserInforBean;
import com.huiyujinrong.qiangdanbao.model.UserInforModel;
import com.huiyujinrong.qiangdanbao.network.Api;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import javax.inject.Inject;

import happyloan.core.RespEvent;


public class UserInforPresenter implements UserInforContacrt.Presenter {

    @Inject
    UserInforModel model;

    private UserInforContacrt.View view;

    public UserInforPresenter(UserInforContacrt.View view) {
        this.view = view;
        setDagger2();
    }

    private void setDagger2() {
        DaggerModelComponent.builder().appComponent(BorrowHelpApp.get().getAppComponent()).build().inject(this);
    }

    @Override
    public void getUserInfor(String user_id) {
        model.getUserInforDta(user_id);
    }

    /**
     * 查询是否认识识别
     *
     * @param user_id
     */
    @Override
    public void findZmxyFaceRecordByUserId(String user_id) {
        model.findZmxyFaceRecordByUserId(user_id);
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void findZmxyFaceRecord(RespEvent<FaceFindBean> event) {
        if (event.getUrl().equals(Api.findZmxyFaceRecordByUserId)) {
            switch (event.getCode()) {
                case RespEvent.Code.SUCCEED:
                    Log.i("findZmxyFaceRecord", "findZmxyFaceRecord: " + event.getT().getFace_flag());
                    view.findZmxyFaceRecord(event.getT());
                    break;
                case RespEvent.Code.ERROR:
                    break;
                case RespEvent.Code.FAILURE:
                    break;
            }

        }
    }

    @Override
    public void getZmxyFaceEngine(String user_id) {
        model.getZmxyFaceEngine(user_id);

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void getZmxyFaceEngines(RespEvent<FaceEngineBean> event) {
        if (event.getUrl().equals(Api.getZmxyFaceEngine)) {
            switch (event.getCode()) {
                case RespEvent.Code.SUCCEED:
                    view.onFaceEngineResult(event.getT().getZmxy_face_url());
                    break;
                case RespEvent.Code.ERROR:
                    view.showToast(event.getExtra().toString());
                    break;
                case RespEvent.Code.FAILURE:
                    view.showToast(event.getExtra().toString());
                    break;
            }

        }
    }

    @Override
    public void start() {
        EventBus.getDefault().register(this);
    }

    @Override
    public void end() {
        EventBus.getDefault().unregister(this);
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void respEvent(RespEvent<UserInforBean> event) {
        if (event.getUrl().equals(Api.selectUinfor)) {
            switch (event.getCode()) {
                case RespEvent.Code.SUCCEED:
                    //数据返回成功
                    view.getUserInforData(event.getT());
                    break;
                case RespEvent.Code.FAILURE:
                    //数据返回错误
                    if (event.isAlert()) {
                        view.showToast(String.valueOf(event.getExtra()));
                    }
                    break;
                case RespEvent.Code.ERROR:
                    //数据返回异常
                    view.showToast("网络不好,稍后再试");
                    break;
            }
        }
    }
}
